# 04_jes2004_wave6.R
# Purpose: Ideological Extremism and Political Participation in Japan
# Created: 2020-5-16 Taka-aki Asano
# Last Modified: 2021-10-9

# package
require("haven")
require("dplyr")
require("ltm")
require("plink")


# dataset
JES2004 <- read.table(
  "JESIII/Japanese/0530β.dat", 
  header = TRUE, sep = "\t", quote = "\"", comment.char = ""
)
JES2004$ID <- 1:nrow(JES2004)


# respondent
JES2004_Respondent <- JES2004[,c("ID", paste0("G", 16:23, "S1"))]
colnames(JES2004_Respondent)[-1] <- paste0("q", 16:23)
JES2004_Respondent <- apply(JES2004_Respondent, c(1, 2), 
                            function(x) {
                              if(x%in%c(8, 9)) {x <- NA} 
                              else {x <- x}}
) %>% as.data.frame()


# IRT
## handle missing data
JES2004_Respondent <- JES2004_Respondent[rowSums(JES2004_Respondent[,-1], na.rm = TRUE) != 0,]

## estimate item parameters
irt2004 <- ltm::grm(JES2004_Respondent[,-1], IRT.param = FALSE)
irt2004 ## viewing

## estimate voters ideology
score2004_Respondent <- ltm::factor.scores(irt2004, JES2004_Respondent[,-1])


# rescale
## common item
common2012 <- c("q8", "q9", "q10", "q12", "q13", "q14")
common2004 <- c("q16", "q17", "q18", "q20", "q21", "q22")
index <- data.frame(
  group1 = match(common2012, colnames(JES2012_Respondent[,-1])), 
  group2 = match(common2004, colnames(JES2004_Respondent[,-1]))
)

## list of parameters
### 2012
res2012 <- coef(irt2012)
res2012 <- res2012[,c(4,1:3)]
colnames(res2012) <- c("a","b1","b2","b3")
### 2004
res2004 <- coef(irt2004)
res2004 <- res2004[,c(4,1:3)]
colnames(res2004) <- c("a","b1","b2","b3")
### merge
pm <- list(res2012, res2004)

## list of scale
rescat <- list(rep(4, nrow(res2012)), 
               rep(4, nrow(res2004)))

## as.irt.pars
pm2012 <- as.poly.mod(n = nrow(res2012), model = "grm", 
                      items = 1:nrow(res2012))
pm2004 <- as.poly.mod(n = nrow(res2004), model = "grm", 
                      items = 1:nrow(res2004))
res <- as.irt.pars(pm, common = index, cat = rescat, 
                   poly.mod = list(pm2012, pm2004), 
                   location = FALSE)

## list of voters' position
ideology <- list(score2012_Respondent$score.dat$z1, 
                 score2004_Respondent$score.dat$z1)

## rescale
plink_out <- plink(res, common = index, base.grp = 1, 
                   rescale = "MM", ability = ideology)
summary(plink_out)

## estimate voters ideology (rescale)
score2004_Respondent_plink <- link.ability(plink_out)$group2
JES2004_Respondent$Ideology <- -1 * score2004_Respondent_plink


# LDP's position
JES2004_LDP <- JES2004[,c("ID", paste0("G", 16:23, "S3X1"))]
colnames(JES2004_LDP)[-1] <- paste0("q", 16:23)
JES2004_LDP <- apply(JES2004_LDP, c(1, 2), 
                     function(x) {
                       if(x%in%c(8, 9)) {x <- NA}
                       else {x <- x}}
) %>% as.data.frame()
JES2004_LDP <- JES2004_LDP[rowSums(JES2004_LDP[,-1], na.rm = TRUE) != 0,]
score2004_LDP <- ltm::factor.scores(irt2004, JES2004_LDP[,-1])
ideology <- list(score2012_Respondent$score.dat$z1, 
                 score2004_LDP$score.dat$z1)
plink_out <- plink(res, common = index, base.grp = 1, 
                   rescale = "MM", ability = ideology)
JES2004_LDP$Ideology <- -1 * link.ability(plink_out)$group2


# DPJ's position
JES2004_DPJ <- JES2004[,c("ID", paste0("G", 16:23, "S3X2"))]
colnames(JES2004_DPJ)[-1] <- paste0("q", 16:23)
JES2004_DPJ <- apply(JES2004_DPJ, c(1, 2), 
                     function(x) {
                       if(x%in%c(8, 9)) {x <- NA}
                       else {x <- x}}
) %>% as.data.frame()
JES2004_DPJ <- JES2004_DPJ[rowSums(JES2004_DPJ[,-1], na.rm = TRUE) != 0,]
score2004_DPJ <- ltm::factor.scores(irt2004, JES2004_DPJ[,-1])
ideology <- list(score2012_Respondent$score.dat$z1, 
                 score2004_DPJ$score.dat$z1)
plink_out <- plink(res, common = index, base.grp = 1, 
                   rescale = "MM", ability = ideology)
JES2004_DPJ$Ideology <- -1 * link.ability(plink_out)$group2
